package com.xiaoxu.intranetweb.dom4jUtil.config;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Component
public class SystemConfigEditer {
//    每天0点切换公司电厂
public void updateSwitchGsOrDc(){
//        更新supplyUnit里的内容为公司
    try{
        SAXReader reader = new SAXReader();
        String path = System.getProperty("user.dir");
        Document document = reader.read(new File(path + "/config/systemConfig.config"));
        Element root = document.getRootElement();
//  supplyUnit为直接标签
        List<Element> coList = root.elements("switchGsOrDc");
//            supplyUnit里面直接存公司两个字
        for (int i = 0; i < coList.size(); i++) {
            Element element = coList.get(i);
//                如果是公司就更新为电厂，如果是电厂就更新为公司
            if (element.getText().equals("b587f98d84174e3e8a811d746ccec712")){//公司
                element.setText("15a2ac2ce6dd49bf8e87cdb16e4c50e3");//电厂
            }
            else {
                element.setText("b587f98d84174e3e8a811d746ccec712");
            }
            OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path + "/config/systemConfig.config"), StandardCharsets.UTF_8);
            document.write(writer);
            writer.close();
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
}
